package com.zg.job.service.impl;

import com.google.common.collect.Maps;
import com.zg.common.core.dao.system.SysJob;
import com.zg.common.core.dao.system.SysJobDao;
import com.zg.common.core.util.SpringUtil;
import com.zg.job.quartz.AJobLauncher;
import com.zg.job.quartz.CommonJobLauncher;
import com.zg.job.service.BatchService;
import com.zg.job.syscode.Constant;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/3/4 15:33
 */
@Slf4j
@Service
@AllArgsConstructor
public class BatchServiceImpl implements BatchService {


    private SysJobDao taskService;

    private JobLauncher jobLauncher;


    @Override
    public void doJob(Long jobId) throws ClassNotFoundException{
        SysJob job = taskService.getById(jobId);
        SysJob sysJob = gen(job);
        AJobLauncher aJobLauncher = SpringUtil.getBean(CommonJobLauncher.class);
        Map<String, JobParameter> jobParameterMap = Maps.newHashMap();
        jobParameterMap.put("runTime", new JobParameter(System.currentTimeMillis()));
        JobParameters jobParameters = new JobParameters(jobParameterMap);
        aJobLauncher.executeBusinessLogic(jobLauncher,sysJob,jobParameters);
    }
    @Override
    public SysJob gen(SysJob sysJob) throws ClassNotFoundException {
//        Class clazz = Class.forName(sysJob.getClassPath());
//        sysJob.setJobClass(clazz);
        Map<String, Object> params = Maps.newHashMap();
        params.put(Constant.DEFAULT_PARAMS_KEY, sysJob.getJobParams());
        sysJob.setJobParams(params);
        return sysJob;

    }
}
